package com.xungou.good.service.impl;

import com.xungou.core.mvc.AbstractService;
import com.xungou.core.mvc.IDao;
import com.xungou.good.dao.IGood;
import com.xungou.good.pojo.Good;
import com.xungou.good.service.IGoodService;
import com.xungou.goodtype.service.IGoodTypeService;
import com.xungou.resource.service.IResourceService;
import com.xungou.shop.service.IShopService;
import com.xungou.utils.FileUtil;
import com.xungou.utils.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;


@Service("goodService")
public class GoodServiceImpl extends AbstractService<Good> implements IGoodService {
    @Resource
    private IGood iGood;
    @Resource
    private IShopService shopService;
    @Resource
    private IResourceService iResourceService;
    @Resource
    private IGoodTypeService iGoodTypeService;

    @Override
    protected IDao<Good> getDao() {
        return iGood;
    }

    @Override
    public Integer getMaxId(Integer type) {
        return iGood.getMaxId(type);
    }

    @Override
    public Map<String, Object> add(MultipartFile file, Good good, Map<String, String> params) {
        com.xungou.resource.pojo.Resource resource = FileUtil.uploadFile(file, "good", iResourceService);
        good.setPhoto(resource.getId());
        good.setSalesVolume(0);
        good.setIsShelves(false);
        good.setShop(shopService.getShopByUserId(UserUtil.getCurrentUser().getId()).getId());

        Integer maxGoodId = getMaxId(good.getType());
        if (maxGoodId == null)
            maxGoodId = 1;
        String code = maxGoodId + "";
        while (code.length() < 6)
            code = "0" + code;
        code = iGoodTypeService.selectByPrimaryKey(good.getType()).getCode() + code;
        good.setCode(code);
        int flag = insertSelective(good);

        if (params.get("propertyValues").length() > 0) {
            List<String> propertyValues = Arrays.asList(params.get("propertyValues").split(","));
            Map<String, Object> temp = new HashMap<String, Object>();
            temp.put("propertyValues", propertyValues);
            temp.put("goodId", good.getId());
            iGood.insertPropertyValues(temp);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", flag);
        return map;
    }

    @Override
    public List<Good> list(Map<String, String> params) {
        return iGood.list(params);
    }

    @Override
    public Map<String, Object> search(Map<String, Object> params) {
        if (params.get("goodTypePropertyValue") == null || params.get("goodTypePropertyValue").toString().equals("")) {
            params.put("goodTypePropertyValue", new ArrayList<String>());
        } else {
            String[] goodTypePropertyValue = params.get("goodTypePropertyValue").toString().split(",");
            params.put("goodTypePropertyValue", Arrays.asList(goodTypePropertyValue));
        }
        if(params.get("page")==null)
            params.put("limit","limit 0,25");
        else{
            int page = Integer.parseInt(params.get("page").toString());
            params.put("limit","limit "+(page-1)*25+",25");
        }
        List<Map<String,Object>> list = iGood.search(params);
        int count = iGood.searchCount(params);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("maxPage",count%25==0?count/25:count/25+1);
        if(params.get("page")!=null)
            result.put("currentPage",params.get("page"));
        else
            result.put("currentPage",1);
        result.put("goodList",list);
        return result;
    }

    @Override
    public Map<String, Object> getGoodMessage(Map<String, String> params) {
        return iGood.getGoodMessage(params);
    }

    @Override
    public Integer getCommentCountByGoodId(Map<String, Object> params) {
        return iGood.getCommentCountByGoodId(params);
    }

    @Override
    public Integer getGoodCommentCountByGoodId(Map<String, Object> good) {
        return iGood.getGoodCommentCountByGoodId(good);
    }

    @Override
    public Float getAverageScoreByGoodId(Map<String, Object> good) {
        return iGood.getAverageScoreByGoodId(good);
    }
}